package com.phil.xml.parse;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.phi.tuangou.bean.MeiTuanCityBean;

public class MeituanCityHanlder extends DefaultHandler {

	List<MeiTuanCityBean> list;
	MeiTuanCityBean bean;
	StringBuffer buf;
	String preTag;

	public MeituanCityHanlder() {
		list = new ArrayList<MeiTuanCityBean>();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (preTag != null) {
			buf = new StringBuffer();

			if (preTag.equals("id")) {
				String s = new String(ch, start, length);
				buf.append(s);
			} else if (preTag.equals("name")) {
				String s = new String(ch, start, length);
				buf.append(s);
			} else if (preTag.equals("timezone")) {
				String s = new String(ch, start, length);
				buf.append(s);
			} else if (preTag.equals("timezone_offset_gmt")) {
				String s = new String(ch, start, length);
				buf.append(s);
			} else if (preTag.equals("latitude")) {
				String s = new String(ch, start, length);
				buf.append(s);
			} else if (preTag.equals("longitude")) {
				String s = new String(ch, start, length);
				buf.append(s);
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("end parse meituan city");
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if (preTag != null) {
			if (preTag.equals("id")) {
				String id = buf.toString();
				bean.setId(id);
			} else if (preTag.equals("name")) {
				String name = buf.toString();
				bean.setName(name);
			} else if (preTag.equals("timezone")) {
				String timezone = buf.toString();
				bean.setTimezone(timezone);
			} else if (preTag.equals("timezone_offset_gmt")) {
				String timezone_offset_gmt = buf.toString();
				bean.setTimezone_offset_gmt(timezone_offset_gmt);
			} else if (preTag.equals("latitude")) {
				String latitude = buf.toString();
				bean.setLatitude(latitude);
			} else if (preTag.equals("longitude")) {
				String longitude = buf.toString();
				bean.setLongitude(longitude);
			}
			preTag = null;
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		System.out.println("start parse meituan city");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if (qName.equals("division")) {
			bean = new MeiTuanCityBean();
			list.add(bean);
		}
		preTag = qName;
	}
	public List<MeiTuanCityBean> getList(){
		return list;
	}

}
